package com.ant.storage.service.impl;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.UserDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.mapper.CompanyMapper;
import com.ant.storage.mapper.UserMapper;
import com.ant.storage.pojo.Company;
import com.ant.storage.pojo.User;
import com.ant.storage.service.IUserService;
import com.ant.storage.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2025-10-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    CompanyMapper companysMapper;

    @Override
    public ResultVo queryUserOne(String id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = this.getOne(queryWrapper);
        return ResultVo.success("ok",user);
    }

    @Override
    @Transactional
    public Object saveUser(UserDto dto) {

        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("user_name",dto.getContactTel());
        User one = baseMapper.selectOne(wrapper);
        if(one!=null){
            throw new AntException(ResultCode.FAILURE.getResultCode(),"该用户已存在");
        }

        User user=new User();
        user.setUserName(dto.getContactTel());
        user.setUserPwd(dto.getUserPwd());
        user.setReview(1);
        user.setCreationTime(LocalDateTime.now());
        user.setAccountType(dto.getAccountType()==null?1:dto.getAccountType());
        baseMapper.insert(user);

        Company companys=new Company();
        companys.setUserId(user.getId());
        companys.setProvince(dto.getProvince());
        companys.setCity(dto.getCity());
        companys.setCounty(dto.getCounty());
        companys.setAddress(dto.getAddress());
        companys.setCompanyCode(dto.getCompanyCode());
        companys.setCompanyName(dto.getCompanyName());
        companys.setBusinessUrl(dto.getBusinessUrl());
        companys.setPersonName(dto.getPersonName());
        companys.setPersonUrl(dto.getPersonUrl());
        companys.setContact(dto.getContact());
        companys.setContactTel(dto.getContactTel());
        companys.setCreationTime(LocalDateTime.now());
        companysMapper.insert(companys);
        return user;
    }

    @Override
    public User updateUserById(UserDto dto) {
        if(dto.getId()==null){
            throw new AntException("输入id");
        }
        User user=new User();
        user.setUserName(dto.getContactTel());
        user.setUserPwd(dto.getUserPwd());
        user.setReview(1);
        user.setCreationTime(LocalDateTime.now());
        user.setAccountType(dto.getAccountType()==null?1:dto.getAccountType());
        user.setId(dto.getId());
        baseMapper.updateById(user);
        Company companys=new Company();
        companys.setUserId(user.getId());
        companys.setProvince(dto.getProvince());
        companys.setCity(dto.getCity());
        companys.setCounty(dto.getCounty());
        companys.setAddress(dto.getAddress());
        companys.setCompanyCode(dto.getCompanyCode());
        companys.setCompanyName(dto.getCompanyName());
        companys.setBusinessUrl(dto.getBusinessUrl());
        companys.setPersonName(dto.getPersonName());
        companys.setPersonUrl(dto.getPersonUrl());
        companys.setContact(dto.getContact());
        companys.setContactTel(dto.getContactTel());
        companys.setCreationTime(LocalDateTime.now());
        companysMapper.insert(companys);
        return user;
    }

    @Override
    public User selectUserPwd(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        queryWrapper.eq("user_pwd",password);
        User one = this.getOne(queryWrapper);
        one.setToken("1231313131");
        return one;
    }
    @Override
    public List<UserVo> userlist() {
        return  baseMapper.userlist();
    }

    @Override
    public void reviewUser(Integer id) {
        User user1 = baseMapper.selectById(id);
        if(user1==null){
            throw new AntException(201,"用户不存在!");
        }
        User user=new User();
        user.setReview(1);
        user.setReviewTime(LocalDateTime.now());
        user.setReviewName("admin");// 登录账号信息
        user.setIsOpen(0);
        user.setId(id);
        baseMapper.updateById(user);
    }
}
